import java.util.ArrayList;
import java.io.*;
import java.net.*;

/**

 */
public class VMWorker implements Runnable{

    protected Socket clientSocket = null;
    protected int serverId;
    protected ArrayList<VirtualMachine> vmTypeList;
    protected boolean listening=true;

    public VMWorker(Socket clientSocket, int serverId, ArrayList<VirtualMachine> vmTypeList) {
        this.clientSocket = clientSocket;
        this.serverId   = serverId;
        this.vmTypeList   = vmTypeList;        
    }

    public void run() {

        try {

            BufferedReader inFromClient =
               new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(clientSocket.getOutputStream());
            String input;
            String answer=null;
              
            while(this.listening) {
            	try {
		            input = inFromClient.readLine();
		    		input=input.replace(" ", "");	     // remove spaces       
		            if(input.equals("Bye")) {
		            	this.listening=false;
		            	answer="Goodbye";
		            } else {
		    			String [] s=input.split("\\W");
		    			if(s[0].equals("Hello")) {
		    				int vmid=Integer.valueOf(s[1]);
		    				int vmType=Integer.valueOf(s[2]);
		    				System.out.println("vm "+vmid+ " is now online on server "+ this.serverId);	    				
		    				answer="Welcome to vm "+vmTypeList.get(vmType).getname()+":"+vmid+"#";
		    				answer+="Your available resources are : #";
		    				answer+="Memory "+vmTypeList.get(vmType).getmemory()+"#";
		    				answer+="Vcpu "+vmTypeList.get(vmType).getvcpu()+"#";
		    				answer+="Disk "+vmTypeList.get(vmType).getdisk()+"#";
		    				answer+="Lan "+vmTypeList.get(vmType).getlan()+"#";
	
		    				answer+="#To run a job, type \"runJob(jobName, runTime)\" #";
		    				answer+="To disconnect from vm, type \"Bye\" ";		    				
		    			} else if(s[0].equals("runJob")) {
		    				System.out.println(input+ " "+ s[1] +" "+ s[2]);	    				
		    				String jobName=s[1];
		    				int runTime=Integer.valueOf(s[2]);
		    				Thread.sleep(runTime);
		    				answer="Job "+ jobName+" has finished executing";
		    				
		    				answer+="#To run a job, type \"runJob(jobName, runTime)\" #";
		    				answer+="To disconnect from vm, type \"Bye\" ";		    				
		    				
		    			}
		            }
		            System.out.println(answer);
		            outToClient.writeBytes(answer+'\n');
                } catch (InterruptedException e) {
                    //report exception somewhere.
                    e.printStackTrace();
                    answer="Goodbye";
		            outToClient.writeBytes(answer+'\n');
                    this.listening=false;
                }
            }
            outToClient.close();
            inFromClient.close();
        } catch (IOException e) {
            //report exception somewhere.
            e.printStackTrace();
        }

    }
    
}

